package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Medication;
import com.ruoyi.system.service.IMedicationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 药品Controller
 *
 * @author ruoyi
 * @date 2023-02-14
 */
@RestController
@RequestMapping("/system/medication")
public class MedicationController extends BaseController
{
    @Autowired
    private IMedicationService medicationService;

    /**
     * 查询药品列表
     */
    @PreAuthorize("@ss.hasPermi('system:medication:list')")
    @GetMapping("/list")
    public TableDataInfo list(Medication medication)
    {
        startPage();
        List<Medication> list = medicationService.selectMedicationList(medication);
        return getDataTable(list);
    }

    /**
     * 导出药品列表
     */
    @PreAuthorize("@ss.hasPermi('system:medication:export')")
    @Log(title = "药品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Medication medication)
    {
        List<Medication> list = medicationService.selectMedicationList(medication);
        ExcelUtil<Medication> util = new ExcelUtil<Medication>(Medication.class);
        util.exportExcel(response, list, "药品数据");
    }

    /**
     * 获取药品详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:medication:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(medicationService.selectMedicationById(id));
    }

    /**
     * 新增药品
     */
    @PreAuthorize("@ss.hasPermi('system:medication:add')")
    @Log(title = "药品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Medication medication)
    {
        return toAjax(medicationService.insertMedication(medication));
    }

    /**
     * 修改药品
     */
    @PreAuthorize("@ss.hasPermi('system:medication:edit')")
    @Log(title = "药品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Medication medication)
    {
        return toAjax(medicationService.updateMedication(medication));
    }

    /**
     * 删除药品
     */
    @PreAuthorize("@ss.hasPermi('system:medication:remove')")
    @Log(title = "药品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(medicationService.deleteMedicationByIds(ids));
    }
}
